package com.jh.jingfei.main;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jh.jingfei.R;
import com.jh.jingfei.bottom_bar.BottomNavigationBar;
import com.jh.jingfei.main.event.StartBrotherEvent;
import com.jh.jingfei.main.ui.HomeFragment;
import com.jh.jingfei.main.ui.MineFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

/**
 * @author lxb
 */
public class MainFragment extends SwipeBackFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    private BottomNavigationBar bottomNavigationBar;

    private SwipeBackFragment [] tabFragments = new SwipeBackFragment[2];

    private int currentShowIndex = 0;

    public MainFragment() {
    }

    public static MainFragment newInstance(String param1, String param2) {
        MainFragment fragment = new MainFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        bottomNavigationBar = (BottomNavigationBar) view.findViewById(R.id.bottom_navigation_bar);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setParallaxOffset(0.5f);

        if (savedInstanceState == null) {
            tabFragments[0] = HomeFragment.newInstance("", "");
            tabFragments[1] = MineFragment.newInstance("", "");

            loadMultipleRootFragment(R.id.content, 0, tabFragments[0], tabFragments[1]);
        } else {
            tabFragments[0] = findFragment(HomeFragment.class);
            tabFragments[1] = findFragment(MineFragment.class);
        }

        bottomNavigationBar.setOnTabSelectedListener(new BottomNavigationBar.SimpleOnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {
                int lastedShowIndex = currentShowIndex;
                currentShowIndex = position;
                showHideFragment(tabFragments[currentShowIndex], tabFragments[lastedShowIndex]);
            }
        });
    }

    @Subscribe
    public void startBrother(StartBrotherEvent event) {
        start(event.targetFragment);
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}
